home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in Java / c13 / AutoEvent.java < prev    next >
Encoding:
Java Source  |  2000-05-25  |  5.7 KB  |  173 lines

  1. //: AutoEvent.java
  2. //////////////////////////////////////////////////
  3. // Copyright (c) Bruce Eckel, 1998
  4. // Source code file from the book "Thinking in Java"
  5. // All rights reserved EXCEPT as allowed by the
  6. // following statements: You can freely use this file
  7. // for your own work (personal or commercial),
  8. // including modifications and distribution in
  9. // executable form only. Permission is granted to use
  10. // this file in classroom situations, including its
  11. // use in presentation materials, as long as the book
  12. // "Thinking in Java" is cited as the source. 
  13. // Except in classroom situations, you cannot copy
  14. // and distribute this code; instead, the sole
  15. // distribution point is http://www.BruceEckel.com 
  16. // (and official mirror sites) where it is
  17. // freely available. You cannot remove this
  18. // copyright and notice. You cannot distribute
  19. // modified versions of the source code in this
  20. // package. You cannot use this file in printed
  21. // media without the express permission of the
  22. // author. Bruce Eckel makes no representation about
  23. // the suitability of this software for any purpose.
  24. // It is provided "as is" without express or implied
  25. // warranty of any kind, including any implied
  26. // warranty of merchantability, fitness for a
  27. // particular purpose or non-infringement. The entire
  28. // risk as to the quality and performance of the
  29. // software is with you. Bruce Eckel and the
  30. // publisher shall not be liable for any damages
  31. // suffered by you or any third party as a result of
  32. // using or distributing software. In no event will
  33. // Bruce Eckel or the publisher be liable for any
  34. // lost revenue, profit, or data, or for direct,
  35. // indirect, special, consequential, incidental, or
  36. // punitive damages, however caused and regardless of
  37. // the theory of liability, arising out of the use of
  38. // or inability to use software, even if Bruce Eckel
  39. // and the publisher have been advised of the
  40. // possibility of such damages. Should the software
  41. // prove defective, you assume the cost of all
  42. // necessary servicing, repair, or correction. If you
  43. // think you've found an error, please email all
  44. // modified files with clearly commented changes to:
  45. // Bruce@EckelObjects.com. (Please use the same
  46. // address for non-code errors found in the book.)
  47. /////////////////////////////////////////////////
  48.  
  49. // Alternatives to action()
  50. import java.awt.*;
  51. import java.applet.*;
  52. import java.util.*;
  53.  
  54. class MyButton extends Canvas {
  55.   AutoEvent parent;
  56.   Color color;
  57.   String label;
  58.   MyButton(AutoEvent parent, 
  59.            Color color, String label) {
  60.     this.label = label;
  61.     this.parent = parent;
  62.     this.color = color;
  63.   }
  64.   public void paint(Graphics  g) {
  65.     g.setColor(color);
  66.     int rnd = 30;
  67.     g.fillRoundRect(0, 0, size().width, 
  68.                     size().height, rnd, rnd);
  69.     g.setColor(Color.black);
  70.     g.drawRoundRect(0, 0, size().width, 
  71.                     size().height, rnd, rnd);
  72.     FontMetrics fm = g.getFontMetrics();
  73.     int width = fm.stringWidth(label);
  74.     int height = fm.getHeight();
  75.     int ascent = fm.getAscent();
  76.     int leading = fm.getLeading();
  77.     int horizMargin = (size().width - width)/2;
  78.     int verMargin = (size().height - height)/2;
  79.     g.setColor(Color.white);
  80.     g.drawString(label, horizMargin, 
  81.                  verMargin + ascent + leading);
  82.   }
  83.   public boolean keyDown(Event evt, int key) {
  84.     TextField t = 
  85.       (TextField)parent.h.get("keyDown");
  86.     t.setText(evt.toString());
  87.     return true;
  88.   }
  89.   public boolean keyUp(Event evt, int key) {
  90.     TextField t = 
  91.       (TextField)parent.h.get("keyUp");
  92.     t.setText(evt.toString());
  93.     return true;
  94.   }
  95.   public boolean lostFocus(Event evt, Object w) {
  96.     TextField t = 
  97.       (TextField)parent.h.get("lostFocus");
  98.     t.setText(evt.toString());
  99.     return true;
  100.   }
  101.   public boolean gotFocus(Event evt, Object w) {
  102.     TextField t = 
  103.       (TextField)parent.h.get("gotFocus");
  104.     t.setText(evt.toString());
  105.     return true;
  106.   }
  107.   public boolean 
  108.   mouseDown(Event evt,int x,int y) {
  109.     TextField t = 
  110.       (TextField)parent.h.get("mouseDown");
  111.     t.setText(evt.toString());
  112.     return true;
  113.   }
  114.   public boolean 
  115.   mouseDrag(Event evt,int x,int y) {
  116.     TextField t = 
  117.       (TextField)parent.h.get("mouseDrag");
  118.     t.setText(evt.toString());
  119.     return true;
  120.   }
  121.   public boolean 
  122.   mouseEnter(Event evt,int x,int y) {
  123.     TextField t = 
  124.       (TextField)parent.h.get("mouseEnter");
  125.     t.setText(evt.toString());
  126.     return true;
  127.   }
  128.   public boolean 
  129.   mouseExit(Event evt,int x,int y) {
  130.     TextField t = 
  131.       (TextField)parent.h.get("mouseExit");
  132.     t.setText(evt.toString());
  133.     return true;
  134.   }
  135.   public boolean 
  136.   mouseMove(Event evt,int x,int y) {
  137.     TextField t = 
  138.       (TextField)parent.h.get("mouseMove");
  139.     t.setText(evt.toString());
  140.     return true;
  141.   }
  142.   public boolean mouseUp(Event evt,int x,int y) {
  143.     TextField t = 
  144.       (TextField)parent.h.get("mouseUp");
  145.     t.setText(evt.toString());
  146.     return true;
  147.   }
  148. }
  149.  
  150. public class AutoEvent extends Applet {
  151.   Hashtable h = new Hashtable();
  152.   String[] event = {
  153.     "keyDown", "keyUp", "lostFocus", 
  154.     "gotFocus", "mouseDown", "mouseUp", 
  155.     "mouseMove", "mouseDrag", "mouseEnter", 
  156.     "mouseExit"
  157.   };
  158.   MyButton 
  159.     b1 = new MyButton(this, Color.blue, "test1"),
  160.     b2 = new MyButton(this, Color.red, "test2");
  161.   public void init() {
  162.     setLayout(new GridLayout(event.length+1,2));
  163.     for(int i = 0; i < event.length; i++) {
  164.       TextField t = new TextField();
  165.       t.setEditable(false);
  166.       add(new Label(event[i], Label.CENTER)); 
  167.       add(t);
  168.       h.put(event[i], t);
  169.     }
  170.     add(b1);
  171.     add(b2);
  172.   }
  173. } ///:~